package com.orders.util;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.orders.R;
import com.orders.activity.CanzhuoActivity;
import com.orders.activity.CanzhuoViewActivity;
import com.orders.activity.LoginActivity;
import com.orders.activity.MainActivity;
import com.orders.activity.TabHostActivity;
import com.orders.activity.UserinfoActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DrawQRActivity extends Activity {

	private static ImageView qriv;
	private TextView qrtn;
	static Bitmap bmp;
	static DrawQRActivity context;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drawqr);

		qriv = (ImageView) findViewById(R.id.qr_iv);
		qrtn = (TextView) findViewById(R.id.qrtrack_tv);
	 context = DrawQRActivity.this;
		

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String string=bundle.getString("track_number");
		String content=bundle.getString("ciphertext");
		Log.i("yang","drawQR content"+content);


		qrtn.setText("餐桌信息");
		createQRImage(content, 800, 800, null);

		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				
				Intent intent = new Intent(DrawQRActivity.this ,TabHostActivity.class);
				DrawQRActivity.this.startActivity(intent);
				DrawQRActivity.this.finish();
			}
		}, 5000);
		
	}

	public static boolean createQRImage(String content, int widthPix, int heightPix, Bitmap logoBm) {
	
		if (content == null || "".equals(content)) {
			return false;
		}
		
		// ���ò���
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		// ���ö�ά���Ŵ��ʣ���ѡL(7%)��M(15%)��Q(25%)��H(30%)���Ŵ���Խ�߿ɴ洢����ϢԽ�٣����Զ�ά�������ȵ�Ҫ��ԽС
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		// ���ÿհױ߾�Ŀ��
		// hints.put(EncodeHintType.MARGIN, 2); //default is 4

		// ͼ������ת����ʹ���˾���ת��
		BitMatrix bitMatrix = null;
		try {
			bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints);

		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[] pixels = new int[widthPix * heightPix];
		// �������ﰴ�ն�ά����㷨��������ɶ�ά���ͼƬ��
		// ����forѭ����ͼƬ����ɨ��Ľ��
		for (int y = 0; y < heightPix; y++) {
			for (int x = 0; x < widthPix; x++) {
				if (bitMatrix.get(x, y)) {
					pixels[y * widthPix + x] = 0xff000000;
				} else {
					pixels[y * widthPix + x] = 0xffffffff;
				}
			}
		}

	
		// ���ɶ�ά��ͼƬ�ĸ�ʽ��ʹ��ARGB_8888
		Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
		bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);

		if (logoBm != null) {
			bitmap = addLogo(bitmap, logoBm);
			bmp=bitmap;
		
			
		}

		qriv.setImageBitmap(bitmap);
		saveBitmapToSDCard(bitmap,"bill");
		
		// ����ʹ��compress������bitmap���浽�ļ����ٽ��ж�ȡ��ֱ�ӷ��ص�bitmap��û���κ�ѹ���ģ��ڴ����ľ޴�
		return true;
		// return bitmap != null &&
		// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new
		// FileOutputStream(filePath));

	}

	/**
	 * �ڶ�ά���м����Logoͼ��
	 */
	private static Bitmap addLogo(Bitmap src, Bitmap logo) {
		if (src == null) {
			return null;
		}

		if (logo == null) {
			return src;
		}

		// ��ȡͼƬ�Ŀ��
		int srcWidth = src.getWidth();
		int srcHeight = src.getHeight();
		int logoWidth = logo.getWidth();
		int logoHeight = logo.getHeight();

		if (srcWidth == 0 || srcHeight == 0) {
			return null;
		}

		if (logoWidth == 0 || logoHeight == 0) {
			return src;
		}

		// logo��СΪ��ά�������С��1/5
		float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
		Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
		try {
			Canvas canvas = new Canvas(bitmap);
			canvas.drawBitmap(src, 0, 0, null);
			canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
			canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);

			canvas.save(Canvas.ALL_SAVE_FLAG);
			canvas.restore();
		} catch (Exception e) {
			bitmap = null;
			e.getStackTrace();
		}
		return bitmap;
	}
	
	
	public static String saveBitmapToSDCard(Bitmap bitmap, String imagename) {  
		Log.i("yang","保存图片");
	       String path =Environment.getExternalStorageDirectory() + "/"+ imagename + ".jpg"; 
	       Log.i("yang",Environment.getExternalStorageDirectory() + "/"+ imagename + ".jpg"); 
	       
	       context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(new File(path))));
	       
	       Toast.makeText(context,  "二维码保存成功", Toast.LENGTH_LONG).show();
	       FileOutputStream fos = null;  
	       try {  
	           fos = new FileOutputStream(path);  
	           if (fos != null) {  
	               bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);  
	               fos.close();  
	           }  
	  
	           return path;  
	       } catch (Exception e) {  
	           e.printStackTrace();  
	       }  
	       return null;  
	   }  
	
	
	
}
